### Generic unix
ARCH = unix
### Win32
# ARCH = win
### DOS
# ARCH = dos

#for dos/win change colon to semicolon below
VPATH = ./JSort:./RomSort:./arch/$(ARCH)
PATHFLAGS = -I../ -I../../

# implicit pattern rule for *.cpp - default C++ file is *.cc or *.C
%o : %.cpp
	$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $< -o $@

# The C++ compiler - this variable is used in the implicit rule for producing
# N.o from N.cc (or N.C)
CXX = g++

# The C compiler - this is used in two implicit rules:
# - the rule for producing N.o from N.c
# - the rule to link a single object file.
CC = gcc

# Preprocessor flags
CPPFLAGS =  # -DNDEBUG when the debugging code as given in section 8.3
            # has to be turned of

# Warning flags for C++ programs
WARNCXXFLAGS = $(WARNCFLAGS) -Wold-style-cast -Woverloaded-virtual
# Debugging flags
DBGCXXFLAGS = -g
# Optimisation flags. Usually you should not optimise until you have finished
# debugging, except when you want to detect dead code.
OPTCXXFLAGS = # -O2

# CXXFLAGS is used in the implicit rule for producing N.o from N.cc (or N.C)
CXXFLAGS = $(WARNCXXFLAGS) $(DBGCXXFLAGS) $(OPTCXXFLAGS) $(PATHFLAGS)

# The linker flags and the libraries to link against.
# These are used in the implicit rule for linking using a single object file;
LDFLAGS = -g
# Use Electric Fence to track down memory allocation problems.
# Link with zlib (libz)
# LOADLIBS = -lefence -lz
LOADLIBS = -lz

# OS-specific parts - include here so that we can do 'CXXFLAGS += ____' if needed
include makefile.$(ARCH)

# The program to build
PROGRAMS = romsort$(EXE) jsort$(EXE)

JDIR = JSort
ROMDIR = RomSort
ARCHDIR = arch/$(ARCH)

# All the .cpp files
SOURCES =   $(wildcard *.cpp)
OSSOURCES = $(wildcard $(ARCHDIR)/*.cpp)
RSOURCES =  $(wildcard $(ROMDIR)/*.cpp)
JSOURCES =  $(wildcard $(JDIR)/*.cpp)

# And the corresponding .o files
OBJECTS =   $(SOURCES:.cpp=.o)
OSOBJECTS = $(OSSOURCES:.cpp=.o)
ROBJECTS =  $(RSOURCES:.cpp=.o)
JOBJECTS =  $(JSOURCES:.cpp=.o)


# first one is the default.  Don't wanna make all.
# "all".
# all:    depend TAGS $(PROGRAMS)

romsort: CXXFLAGS += -DNOSKIPGENHDR
romsort:   $(OBJECTS) $(OSOBJECTS) $(ROBJECTS)
	$(CXX) $(LDFLAGS) $(LOADLIBS) -o romsort$(EXE) $(OBJECTS) $(OSOBJECTS) $(ROBJECTS)

jsort: CXXFLAGS += -DJSORT -DJSHERIFF
jsort:     $(OBJECTS) $(OSOBJECTS) $(JOBJECTS)
	$(CXX) $(LDFLAGS) $(LOADLIBS) -o jsort$(EXE) $(OBJECTS) $(OSOBJECTS) $(JOBJECTS)

# "clean" removes files not needed after a build.
clean:
	-rm -f $(OBJECTS) $(JOBJECTS) $(ROBJECTS) $(OSOBJECTS)

# "realclean" removes everything that's been built.
realclean: clean
	-rm -f $(PROGRAMS) .depend TAGS

# "force" forces a full rebuild.
force:
	$(MAKE) realclean
	$(MAKE)

# "depend" calculates the dependencies.
depend:
	rm -f .depend
	$(MAKE) .depend

# This is the actual dependency calculation.
.depend: $(SOURCES) # $(OSSOURCES) $(JSOURCES) $(RSOURCES)
	rm -f .depend
# For each source, let the compiler run the preprocessor with the -M and -MM
# options, which causes it to output a dependency suitable for make.
# Note: cd should be spawned in a new shell

	for source in $(SOURCES) ; do \
	  $(CXX) $(CPPFLAGS) -M -MM $$source | tee -a .depend ; \
	done

	cd $(ARCHDIR) ; for source in $(notdir $(OSSOURCES)) ; do \
	  $(CXX) $(CPPFLAGS) -M -MM $$source | tee -a .depend ; \
	done

	cd $(ROMDIR) ; for source in $(notdir $(RSOURCES)) ; do \
	  $(CXX) $(CPPFLAGS) -M -MM $$source | tee -a .depend ; \
	done

	cd $(JDIR) ; for source in $(notdir $(JSOURCES)) ; do \
	  $(CXX) $(CPPFLAGS) -M -MM $$source | tee -a .depend ; \
	done

# Include the generated dependencies.
ifneq ($(wildcard .depend),'')
include .depend
# include $(JDIR)/.depend
# include $(ROMDIR)/.depend
# include $(ARCHDIR)/.depend
endif

# Produce a `TAGS' file for (x)emacs to use for browsing the source code.
TAGS:   $(SOURCES)
	etags -o TAGS --typedefs-and-c++ $(SOURCES)


# Tell make that "all" etc. are phony targets, i.e. they should not be confused
# with files of the same names.
.PHONY: clean realclean force depend
